package ws

import (
	"sync"
)

type (
	onMessageFunc  func(cli *Client, request *Request) // 处理ws被动接收消息的方法
	onSysBroadcast func(request *SysBroadcast)         // 处理ws系统广播消息的方法
)

var (
	handlersMsg        = make(map[string]onMessageFunc)
	handlersMsgRWMutex sync.RWMutex

	handlersSysBroadcast = make(map[string]onSysBroadcast)
	handlersSysRWMutex   sync.RWMutex
)

// 获取路由方法
func getHandlersMsg(key string) (handler onMessageFunc, ok bool) {
	handlersMsgRWMutex.RLock()
	defer handlersMsgRWMutex.RUnlock()
	handler, ok = handlersMsg[key]
	return
}

// 注册websocket方法
func RegisterMsg(key string, handler onMessageFunc) {
	handlersMsgRWMutex.Lock()
	defer handlersMsgRWMutex.Unlock()
	handlersMsg[key] = handler
}

// 获取系统广播路由方法
func getHandlersSysBroadcast(key string) (handler onSysBroadcast, ok bool) {
	handlersSysRWMutex.RLock()
	defer handlersSysRWMutex.RUnlock()
	handler, ok = handlersSysBroadcast[key]
	return
}

// 注册系统广播处理方法
func RegisterSysBroadcast(key string, handler onSysBroadcast) {
	handlersSysRWMutex.Lock()
	defer handlersSysRWMutex.Unlock()
	handlersSysBroadcast[key] = handler
}